package com.henry.others;

import java.util.Scanner;

public class DaysOfMonth {
    /**
     * Judge whether a year is a leap year or not.
     * @param year: The number of the year.
     * @return returns true if it is a leap year.
     */
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    /**
     * Calculate the number of days in a month of a certain year;
     * @param year: The number of the month.
     * @param month: The month of which to find out how many days are there.
     * @return How many days are there in 'month' in the year of 'year'.
     */
    public static int daysOfMonth(int year, int month) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (month == 2 && isLeapYear(year)) return 29;
        else return days[month - 1];
    }

    public static boolean validYear(int year) {
        return year > 1900 && year < 3000;
    }

    public static boolean validMonth(int month) {
        return month > 0 && month < 13;
    }

    public static void main(String[] args) {
        String[] engMonthStrings = {"January", "February", "March", "April", "May",
                "June", "July", "August", "September", "October", "November", "December"
        };

        Scanner keyboard = new Scanner(System.in);

        int year, month;

        do {
            System.out.print("Enter the year: ");
            year = keyboard.nextInt();
            if (!validYear(year)) System.out.println("Invalid year! Please try again. ");
        } while (!validYear(year));

        do {
            System.out.print("Enter the month: ");
            month = keyboard.nextInt();
            if (!validMonth(month)) System.out.println("Invalid month! Please try again. ");
        } while (!validMonth(month));

        System.out.println(
                engMonthStrings[month - 1] + ", " + year + " has " + daysOfMonth(year, month) + " days."
        );
    }
}
